/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
    
    $.fn.init_search_friend=function(){
        $own_id = $("#hdf_own_id").val();
        $("[class=add_friend]").each(function(index) {
            values = $(this).attr("id").split("-");
            $owned_id = $.trim($("#hdf_owned_id").val());
            if($own_id == values[0]){
                $($(this)).text("");
                $($(this)).attr("style", "display:none");
            }
            if(values.length == 6){
                //values[0] member id
                //values[1] owned id
                $.post(
                        $("#global_url").attr("value") + "/index.php?act=regportal&&CODE=is-friend", 
                        {member_id: values[0], friends_id: values[1], index_item: index},
                        function(data){
                            datas = $.trim(data).split("_");
                            if($.trim(datas[2]) == $.trim(datas[3])){
                                $($('#friendlist').find('.add_friend').get(datas[1])).text("").attr("style", "display:none");                        
                            }else if(datas[0]=="1"){
                                $($('#friendlist').find('.add_friend').get(datas[1])).text("Hủy"); 
                            }else if(datas[0] == "0"){
                                $($('#friendlist').find('.add_friend').get(datas[1])).text("Chờ");                        
                            }else{
                                $($('#friendlist').find('.add_friend').get(datas[1])).text("Mời");                        
                            }
                        }
                    );
            }
        });
        return false;
    }
    
    $.fn.add_friend = function(){
        $("[class=add_friend]").click(function(){ 
        $add_url = $("#global_url").attr("value") + "/index.php?act=regportal&&CODE=own-add-friend";
        if($(this).text()=="Chờ" || $(this).text()==""){
            return false;
        }
        $temp = $(this);
        values = $(this).attr("id").split("-");
        $.post( 
            $add_url,
            {member_id: values[0], friends_id: values[1], text:$(this).text()},
            function(data) {
                if($.trim(data) == "own_add_friend_success"){
                    $temp.text("Chờ");
                }else if($.trim(data) == "owned_removed_success"){
                    $temp.text("Mời");
                }
            },
            'html'
            );
            return false;
    });
    }
    $.fn.view_friend_approve = function(){
        $("[class=member-approve]").click(function(){
            values = $(this).attr("id").split("-");
            //console.log(values);
            $approve_url = $("#global_url").attr("value") + "/index.php?act=regportal&&CODE=";
            $.post( 
                $search_url + "member-approve-friends",
                {id:values[0]},
                function(data) {
                    if($.trim(data) != ""){
                        $("#content").html($.trim(data));
                    }else{
                        alert(empty_invited);
                    }
                },
                'html'
            );
        });
    }
    $.fn.simple_add_friend = function(){
        $("[class=link_add]").click(function(){ 
        $add_url = $("#global_url").attr("value") + "/index.php?act=regportal&CODE=own-add-friend";
        $temp = $(this);
        values = $(this).attr("id").split("_");
        $.post( 
            $add_url,
            {member_id: values[0], friends_id: values[1], text:"invite"},
            function(data) {
                if($.trim(data) == "own_add_friend_success"){
                    $temp.attr("style", "display:none");
                    //$temp.text("Chờ");
                }
            },
            'html'
            );
            return false;
    });
    }
    $.fn.accept_friend = function(){
        $("[class=member-approve]").click(function(){
            values = $(this).attr("id").split("-");
            //console.log(values);
            $approve_url = $("#global_url").attr("value") + "/index.php?act=regportal&&CODE=";
            console.log($approve_url);
            $.post( 
                $search_url + "member-approve-friends",
                {id:values[0]},
                function(data) {
                    console.log(data);
                    if($.trim(data) != ""){
                        $("#content").html($.trim(data));
                    }else{
                        alert(empty_invited);
                    }
                },
                'html'
            );
        });
    }
    $.fn.approves_friend = function(){
        $("[class=approve-friend]").click(function(){
            values = $(this).attr("id").split("-");
            $.post( 
                $search_url + "approve-friend",
                {member_id:values[0],friend_id:values[1]},
                function(data) {
                    //console.log(data);return false;
                    if($.trim(data) != ""){
                        if($.trim(data) =="author_fail"){
                            alert(author_fail);
                        }else if($.trim(data) =="not_friend"){
                            alert(not_friend);
                        }else if($.trim(data) == "owned_approved_success"){
                            alert("owned approved success");
                            window.location.reload();
                        }
                    }
                },
                'html'
            );
            return false;
        });
    }
    $.fn.reject_friend = function(){
        $("[class=reject-friend]").click(function(){
            values = $(this).attr("id").split("-");
            console.log($search_url + "reject-friend");
            $.post( 
                $search_url + "reject-friend",
                {member_id:values[0],friend_id:values[1]},
                function(data) {
                    console.log(data);
                    if($.trim(data) != ""){
                        if($.trim(data) =="author_fail"){
                            alert(author_fail);
                        }else if($.trim(data) =="not_friend"){
                            alert(not_friend);
                        }else if($.trim(data) == "owned_rejected_success"){
                            //alert("owned rejected success");
                            window.location.reload();
                        }
                    }
                },
                'html'
            );
            return false;
        });
    }
    
    $.fn.remove_friend = function(){
        //alert("dfdfdf");
        $("[class=remove_friend]").click(function(){
            values = $(this).attr("id").split("-");
            $.post( 
                $search_url + "remove-friend",
                {member_id:values[0],friend_id:values[1]},
                function(data) {
                    console.log(data);
                    if($.trim(data) != ""){
                        if($.trim(data) =="author_fail"){
                            alert(author_fail);
                        }else if($.trim(data) =="not_friend"){
                            alert(not_friend);
                        }else if($.trim(data) == "owned_removed_success"){
                            window.location.reload();
                        }
                    }
                },
                'html'
            );
            return false;
        });
    }
    $.fn.friends_paging = function(){
        //console.log($('.paging'));
        $('.paging').find('a').each(function(){
            //console.log(this);
            $(this).click(function(){
                
                var url = $(this).attr('href');
                //console.log(url);
                var st = 0;
                values = url.split("&");
                //console.log(values);return false;
                if(values[1] != null){
                    temps = values[1].split("=");
                    st = temps[1];
                }
                //console.log($("#profile_url").val() + $("#profile_name").val() +"?act=profile&&CODE=03&&st="+st);return false;
                //window.location.href=$("#profile_url").val() + $("#profile_name").val() +"?act=profile&&CODE=03&&st="+st;
                $.post( 
                    $("#global_url").attr("value") + "act=profile&&CODE=load-all-friends",
                    //$("#profile_url").val() + "/" + $("#profile_name").val() + "&&st=" + st + "&&CODE=load-all-friends",
                    {st:st, friend_id: $("#profile_id").val()},
                    function(data) {
                        if($.trim(data) != ""){
                            //console.log(data);
                            $("#content").html($.trim(data));                    
                        }
                    },
                    'html'
                );
                return false;
            })
        });
    }
    /* Chung Nguyen Huu
     * friend comment
     * 2011/09/10
     */
    $.fn.friend_change=function(){
        $('#friendComment').live('click',function(){
            $comment = $.trim($("#txtmycomment").attr("value"));
            if($comment == ""){
                $("#commentInfo").empty().html(comment_empty);
                return false;
            }
            if($.trim($comment) == $.trim("Viết comment...")){
                alert(comment_empty);
                return false;
            }
            $url=$("#global_url").attr("value") + "/index.php?act=profile&&CODE=own-comment"; 
            $.post( 
                $url,
                $("#friend_comment").serialize(),
                function(data) {
                    if($.trim(data) != ""){
                        if($.trim(data.split("[$$$$$]")[0]) =="own_comment_success"){
                            comments = $.trim(data).split("[$$$$$]");
                            $("#comments").empty().html(comments[1]);
                            
                        }
                        document.getElementById("txtmycomment").value = "Viết comment...";
                    }
                },
                'html'
                );
             return false;
        });
    }
     /* Chung Nguyen Huu
     * delete own comment
     * 2011/09/10
     */
    $.fn.friend_delete=function(){
        delete_url = $("#search_url").attr("value");
        
        $("[class=but_deleteComment]").click(function(){
            $url = $("#global_url").val()  + "/index.php?act=profile&&CODE=delete-own-comment";
            //confirm delete own comment
            if(confirm(confirm_delete_message)){
            }else{
                return false;
            }
            $.post( 
                $url,
                {id: $(this).attr("id")},
                function(data) {
                    window.location.reload();
                },
                'html'
                );
                return false;
        });
    }
    
    $.fn.friend_comment_paging = function(){
        
        $('.paging').find('a').each(function(index){
            $(this).click(function(){
                var url = $(this).attr('href');
                var st = 0;
                values = url.split("&");
                if(values[1] != null){
                    temps = values[1].split("=");
                    st = temps[1];
                }
                $.post(
                    $("#global_url").attr("value") + "/index.php?act=profile&&CODE=comment-paging",
                    {st:st,profile_id: $("#profile_id").val(), profile_name: $("#profile_name").val()},
                    function(data) {
                        if($.trim(data) != ""){           
                            $("#comments").empty().append(data);                    
                        }
                    },
                    'html'
                );
                return false;
            });
        });
    }
    
    $.fn.friend_view_all_friend = function(){
        
        $("#friend_view_all_friend").click(function(){
            $.post( 
                $("#global_url").attr("value") + "/index.php?act=profile&&CODE=load-all-friends",
                {friend_id:$(this).attr("class")},
                function(data) {
                    if($.trim(data) != ""){
                        //console.log(data);
                        $("#content").html($.trim(data));                    
                    }
                },
                'html'
            );
                return false;
        });
    }
    
    $.fn.view_friend_profile = function(){
        $("#view_friend_profile").live('click',function(){
            //console.log($("#global_url").attr("value") + "act=profile&CODE=load-friend-profile");
            $.post( 
                $("#global_url").attr("value") + "/index.php?act=profile&CODE=load-friend-profile",
                {id: $("#profile_friend_id").val()},
                function(data) {
                    //console.log(data);
                    if($.trim(data) != ""){
                        $("#content").html($.trim(data));                    
                    }
                },'html'
                );
        });
    }
});
